﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data;
using System.Data.SqlClient;

namespace DAO
{
    public class ChiTietThucDonDAO
    {
        public static bool Insert(ChiTietThucDonDTO cttd)
        {
            bool result = false;

            string strSQLEmployee = "";
            int n = 0;

            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@ThucDon", cttd.ThucDon));
                sqlParams.Add(new SqlParameter("@MonAn", cttd.MonAn));

                strSQLEmployee = "Select SoPhanAn From ChiTietThucDon Where ThucDon=@ThucDon and MonAn=@MonAn";
                DataTable dt = SqlDataAccessHelper.ExcuteQuery(strSQLEmployee, sqlParams);

                if (dt.Rows.Count == 0)
                {
                    strSQLEmployee = "Insert into ChiTietThucDon(ThucDon,MonAn,SoPhanAn) values (@ThucDon,@MonAn,@SoPhanAn)";
                    // Create List Sql Parameter
                    sqlParams = new List<SqlParameter>();
                    sqlParams.Add(new SqlParameter("@ThucDon", cttd.ThucDon));
                    sqlParams.Add(new SqlParameter("@MonAn", cttd.MonAn));
                    sqlParams.Add(new SqlParameter("@SoPhanAn", cttd.SoPhanAn));
                    // Call execute none query
                    n = SqlDataAccessHelper.ExcuteNoneQuery(strSQLEmployee, sqlParams);//.ExecuteNoneQuery(strSQLEmployee, sqlParams);
                }
                else
                {
                    strSQLEmployee = "Update ChiTietThucDon set SoPhanAn=@SoPhanAn " +
                        "Where ThucDon=@ThucDon and MonAn=@MonAn";
                    // Create List Sql Parameter
                    sqlParams = new List<SqlParameter>();
                    sqlParams.Add(new SqlParameter("@ThucDon", cttd.ThucDon));
                    sqlParams.Add(new SqlParameter("@MonAn", cttd.MonAn));
                    sqlParams.Add(new SqlParameter("@SoPhanAn", cttd.SoPhanAn + Convert.ToInt32(dt.Rows[0]["SoPhanAn"].ToString())));
                    // Call execute none query
                    n = SqlDataAccessHelper.ExcuteNoneQuery(strSQLEmployee, sqlParams);//.ExecuteNoneQuery(strSQLEmployee, sqlParams);
                }

                if (n == 1)
                    result = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        public static bool Delete(int MaCTTD)
        {
            bool result = false;
            string strSQLEmployee = "DELETE FROM ChiTietThucDon WHERE MaChiTietThucDon=@MaChiTietThucDon";
            try
            {
                // Create List Sql Parameter
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaChiTietThucDon", MaCTTD));
                // Call Store Procedure
                int n = SqlDataAccessHelper.ExcuteNoneQuery(strSQLEmployee, sqlParams);
                if (n == 1)
                    result = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        public static bool DeleteByMaThucDon(int MaTD)
        {
            bool result = false;
            string strSQLEmployee = "DELETE FROM ChiTietThucDon WHERE ThucDon=@ThucDon";
            try
            {
                // Create List Sql Parameter
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@ThucDon", MaTD));
                // Call Store Procedure
                int n = SqlDataAccessHelper.ExcuteNoneQuery(strSQLEmployee, sqlParams);
                if (n >= 1)
                    result = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        public static DataTable SelectAllByMaThucDon(int MaThucDon)
        {
            String strSql = "Select MaChiTietThucDon, MaMonAn, TenMonAn, SoPhanAn From ChiTietThucDon, MonAn " +
                "Where ThucDon = @MaThucDon and ChiTietThucDon.MonAn = MonAn.MaMonAn";

            try
            {
                // Create List Sql Parameter
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaThucDon", MaThucDon));

                DataTable dt = SqlDataAccessHelper.ExcuteQuery(strSql, sqlParams);
                return dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
